<template>
  <div class="wrap">
    <el-container>
      <!-- 头部 -->
      <el-header>
        <div class="logo">
          <img src="../assets/imgs/logo1.jpg" alt="logo">
          <span>图书后台管理系统</span>
        </div>
        <el-button type="info" round @click="logout">退出</el-button>
      </el-header>
      <!-- 主体区 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle_but" @click="toggleCollapse">|||</div>
          <el-menu background-color="#3a3f4e" text-color="#fff" active-text-color="#ffd04b" unique-opened
            :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''" v-for="item of menuList" :key="item.id">
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id"
              @click="saveNavStatus('/' + subitem.path)">
                <i class="el-icon-menu"></i>
                {{ subitem.authName }}
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 内容区 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
 
<script>
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],  // 侧边栏数据
      iconObj: {  // 侧边栏字体图标
        '1': 'iconfont icon-yonghuguanli',  // 用户管理
        '3': 'iconfont icon-quanxianguanli',  // 权限管理
        '6': 'iconfont icon-tushu',  // 图书管理
      },
      isCollapse: false,  // 侧边栏的折叠与展开控制 - 默认折叠
      activePath:'',  // 被激活的链接地址
    }
  },
  created() {
    this.getMenuList(),
    this.activePath = window.localStorage.getItem('activePath')
  },
  methods: {
    logout() {
      this.$confirm('此操作将退出本系统，需要保存登录信息吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
        this.$router.push('/login')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已删除登录信息！'
        });
        this.$router.push('/login')
        window.localStorage.clear()
      });
    },
    async getMenuList() {
      const { data: res } = await this.$ajax.get('side/sidebarList')
      if (res.code == 1) return this.$message.error('侧边栏数据请求失败！')
      this.menuList = res.data
    },
    // 侧边栏的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存路由连接的激活状态
    saveNavStatus(activePath){
      window.localStorage.setItem('activePath',activePath)
      this.activePath = activePath
    },
  }
}
</script>
 
<style lang='less' scoped>
.wrap {
  height: 100%;
}

.el-container {
  height: 100%;
}

.el-header {
  background-color: #292829;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 10px;
  color: rgb(210, 209, 209);

  .logo {
    width: 30%;
    height: 60px;
    display: flex;
    align-items: center;

    img {
      width: 60px;
      height: 60px;
      margin-right: 10px;
    }

    span {
      font-size: 26px;
      font-family: '楷体';
    }
  }

}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.iconfont {
  margin-right: 10px;
}

.el-main {
  background-color: #eaedf1;
}

.toggle_but {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>